#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "QStringUtil.h"
#include "TomcatCheckWorker.h"
#include "MysqlCheckWorker.h"
#include "LogReadWorker.h"
#include "CmdUtil.h"
#include <QIODevice>
#include <QMessageBox>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>

QTimer timer;
TomcatCheckWorker tomcatCheckWorker;
MysqlCheckWorker mysqlCheckWorker;
LogReadWorker logReadWorker;

int MainWindow::tomcatPort = 0;
int MainWindow::mysqlPort = 0;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QFile config("./config.json");
    if (config.open(QIODevice::ReadOnly)) {
        initConfig(config);
        ui->setupUi(this);
        QObject::connect(&timer, SIGNAL(timeout()), &tomcatCheckWorker, SLOT(onTimeout()));
        QObject::connect(&timer, SIGNAL(timeout()), &mysqlCheckWorker, SLOT(onTimeout()));
        QObject::connect(&timer, SIGNAL(timeout()), &logReadWorker, SLOT(onTimeout()));
        QObject::connect(&tomcatCheckWorker, SIGNAL(finish(bool)), this, SLOT(onTomcatCheckReceive(bool)));
        QObject::connect(&mysqlCheckWorker, SIGNAL(finish(bool)), this, SLOT(onMysqlCheckReceive(bool)));
        QObject::connect(&logReadWorker, SIGNAL(finish(QString)), this, SLOT(onLogRead(QString)));
        timer.start(500);
    } else {
      QMessageBox::information(this, "error", "no config file found");
    }
}

void MainWindow::initConfig(QFile &config){
    QByteArray jsonData = config.readAll();
    config.close();
    QJsonParseError jsonError;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonData, &jsonError));
    if (jsonError.error != QJsonParseError::NoError) {
        QMessageBox::information(this, "error", "config file read error");
        return;
    }
    QJsonObject jsonConfig = jsonDoc.object();
    tomcatPort = jsonConfig.value("port").toObject().value("tomcat").toInt();
    mysqlPort = jsonConfig.value("port").toObject().value("mysql").toInt();
}

void MainWindow::onTomcatCheckReceive(bool flag) {
    //qDebug() << "onTomcatCheckReceive";
    qDebug() << flag;
    if (flag) {
        ui->tomcatFlag->setStyleSheet(QString("background: green;"));
    } else {
        ui->tomcatFlag->setStyleSheet(QString("background: red;"));
    }
}
void MainWindow::onMysqlCheckReceive(bool flag) {
    //qDebug() << "onMysqlCheckReceive";
    qDebug() << flag;
    if (flag) {
        ui->mysqlFlag->setStyleSheet(QString("background: green;"));
    } else {
        ui->mysqlFlag->setStyleSheet(QString("background: red;"));
    }
}

void MainWindow::onLogRead(QString str) {
    //qDebug() << "onLogRead";
    ui->plainTextEdit->clear();
    ui->plainTextEdit->setPlainText(str);
    ui->plainTextEdit->moveCursor(QTextCursor::End);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_tomcatStart_clicked()
{
    qDebug() << "on_tomcatStart_clicked";
    qDebug() << CmdUtil::runCmd("cd ./tomcat && startup.bat");
}

void MainWindow::on_tomcatStop_clicked()
{
    qDebug() << "on_tomcatStop_clicked";
    qDebug() << CmdUtil::runCmd("cd ./tomcat && shutdown.bat");
}

void MainWindow::on_mysqlStart_clicked()
{
    qDebug() << "on_mysqlStart_clicked";
    qDebug() << CmdUtil::runCmd("cd ./MySQL && start.bat", 2000);
}

void MainWindow::on_mysqlStop_clicked()
{
    qDebug() << "on_mysqlStop_clicked";
    qDebug() << CmdUtil::runCmd("cd ./MySQL && kill.bat");
}
